package com.yj.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yj.answer.entity.Answer;
import com.yj.answer.entity.Wrongtopic;
import com.yj.answer.mapper.AnswerMapper;
import com.yj.answer.mapper.WrongtopicMapper;
import com.yj.answer.service.IAnswerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 题目 服务实现类
 * </p>
 *
 * @author yj
 * @since 2023-07-07
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private WrongtopicMapper wrongtopicMapper;

    /**
     * 作用: 随机题
     * yinj on 2023/7/7
     * @param num
     * @return List<Answer>
    **/
    @Override
    public List<Answer> RandTopics(Integer num) {
        return answerMapper.RandTopics(num);
    }

    /**
     * 作用: 章节
     * yinj on 2023/7/7
     * @param chapter
     * @return List<Answer>
    **/
    @Override
    public List<Answer> chapterTopics(String chapter) {
        return answerMapper.chapterTopics(chapter);
    }

    /**
     * 作用: 错误题
     * yinj on 2023/7/7
     * @param
     * @return List<Answer>
    **/
    @Override
    public List<Answer> wrongtopic() {
        return answerMapper.wrongtopic();
    }

    /**
     * 作用: 做题
     * yinj on 2023/7/7
     * @param answer
     * @return List<Answer>
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean exercises(Answer answer) {
        answerMapper.topicnum(answer.getId());
        new Thread(()->{
            answerMapper.answerLog(answer.getId(),answer.getAnswer());
        }).start();
        //判断是否正确
        Integer count = answerMapper.selectCount(new QueryWrapper<Answer>().eq("id", answer.getId()).eq("answer", answer.getAnswer()));
        if (count > 0){
            wrongtopicMapper.delete(new QueryWrapper<Wrongtopic>().eq("answer",answer.getId()));
            return true;
        }
        Integer answer1 = wrongtopicMapper.selectCount(new QueryWrapper<Wrongtopic>().eq("answer", answer.getId()));
        if (answer1==0) {
            wrongtopicMapper.insert(new Wrongtopic().setAnswer(answer.getId()));
        }
        return false;
    }

    /**
     * 作用: 首页
     * yinj on 2023/7/8
     * @param
     * @return HashMap<String,Object>
    **/
    @Override
    public HashMap<String, Integer> homepage() {
        Integer count1 = answerMapper.selectCount(new QueryWrapper<Answer>());
        Integer count2 = wrongtopicMapper.selectCount(new QueryWrapper<Wrongtopic>());
        HashMap<String, Integer> map = new HashMap<>();
        map.put("count1",count1);
        map.put("count2",count2);
        return map;
    }

    /**
     * 作用: 章节列表
     * yinj on 2023/7/8
     * @param
     * @return HashMap<String,Integer>
    **/
    @Override
    public List<HashMap<String,Object>> chapterLists() {
       List<String> books = answerMapper.boolLists();
        List<HashMap<String,Object>> data = new ArrayList();
        for (String book : books) {
            HashMap<String,Object> map = new HashMap<>();
            map.put("label",book);
            List<Answer> answers = answerMapper.chapterLists(book);
            List<String> list = new ArrayList();
            for (Answer answer : answers) {
                list.add(answer.getChapter()+"("+answer.getCount()+")");
            }
            map.put("lists", list);
            data.add(map);
        }
        return data;
    }

    public void insertAuestions(Answer answer) {
        Integer topic = answerMapper.selectCount(new QueryWrapper<Answer>().eq("topic", answer.getTopic()));
        if (topic == 0){
            answerMapper.insertAuestions(answer);
        }
    }
}
